package frame.project;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class ProduceImage {
	public Bitmap decodeBitmap(String path, int reqWidth, int reqHeight) {
		Bitmap bm = null;
		
		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options option = new BitmapFactory.Options();
		option.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(path, option);
	     
		// Calculate inSampleSize
		// this will scale the images to match device density
		option.inSampleSize = calculateInSampleSize(option, reqWidth, reqHeight);
	     
		// Decode bitmap with inSampleSize set
		option.inJustDecodeBounds = false;
		bm = BitmapFactory.decodeFile(path, option); 
		
		return bm;  	
	}
	
	// Calculate sampling size
    public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		
		//the higher, the smaller bits we get
		int inSampleSize = 100;
		
		if (height > reqHeight || width > reqWidth) {
			if (width > height) {
				inSampleSize = Math.round((float)height / (float)reqHeight);   	
			} else {
				inSampleSize = Math.round((float)width / (float)reqWidth);   	
			}   
		}
		
		return inSampleSize;   	
	}
    
    public Bitmap decodeScaledBitmap(String path, int reqWidth, int reqHeight){
    	Bitmap bm = decodeBitmap(path, reqWidth, reqHeight);
    	bm = Bitmap.createScaledBitmap(bm, 100, 100, false);
    	return bm;
    }
    
    public Bitmap scaleBitmap(Bitmap bm){
    	return Bitmap.createScaledBitmap(bm, 100, 100, true);
    }
}
